package com.see51.demo.see51demo.Frag;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.see51.demo.see51demo.Model.DevFileInfo;
import com.see51.demo.see51demo.R;
import com.see51.demo.see51demo.See51App;
import com.see51.sdk.java.Jni51SeeSdk;

import java.util.ArrayList;
import java.util.List;


public class DownFileListFragment extends Fragment
{

    DownFileListAdapter    m_FileListAp;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_downfile_list, container, false);

        ListView lst_dir = (ListView) view.findViewById(R.id.downfilelist);
        m_FileListAp = new DownFileListAdapter();


        final See51App app = (See51App) this.getActivity().getApplication();

        app.GetDownFileMap(m_FileListAp.DataList);
        lst_dir.setAdapter(m_FileListAp);
        //lst_dir.setOnItemClickListener( m_DevListAp);
        return view;
    }


    //下载列表适配器
    class DownFileListAdapter extends BaseAdapter implements AdapterView.OnItemClickListener
    {

        private List<DevFileInfo> DataList = new ArrayList<DevFileInfo>();//实体类
        public void Add(DevFileInfo item)
        {
            DataList.add(item);
        }
        public void Clear()
        {
            DataList.clear();
        }
        public int getCount()
        {
            // TODO Auto-generated method stub
            return DataList.size();//数目
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            DevFileInfo Item = DataList.get(position);
            LayoutInflater inflater = DownFileListFragment.this.getActivity().getLayoutInflater();
            View view;

            if (convertView==null) {
                //因为getView()返回的对象，adapter会自动赋给ListView
                view = inflater.inflate(R.layout.down_file_item, null);
            }else{
                view=convertView;
                //Log.i("info","有缓存，不需要重新生成"+position);
            }

            TextView tv1 = (TextView) view.findViewById(R.id.Txt_FileName);//找到Textviewname
            tv1.setText(Item.FileName);

            TextView tv_FileSize = (TextView) view.findViewById(R.id.Txt_FileSize);//找到Textviewname
            tv_FileSize.setText(""+Item.FileSize);

            TextView tv_FileState = (TextView) view.findViewById(R.id.Txt_FileState);//找到Textviewname
            if(Item.DownState==1)
                 tv_FileState.setText("下载中");
            else if(Item.DownState==3)
                tv_FileState.setText("下载完成");
            else if(Item.DownState==4)
                tv_FileState.setText("下载失败");
            return view;
        }
        @Override
        public long getItemId(int position) {//取在列表中与指定索引对应的行id
            return 0;
        }
        @Override
        public Object getItem(int position) {//获取数据集中与指定索引对应的数据项
            return null;
        }

        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {

        }
    }
}
